package com.banyuan.servlet.user;

import com.banyuan.dao.UserDao;
import com.banyuan.domain.User;
import com.banyuan.utils.DBUtils;
import com.banyuan.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/user/updatepage")
public class UpdatePageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        ResultSet rs = null;
        try {
             rs = UserDao.selectUser(Integer.valueOf(id));
            User user = DBUtils.resultToBeanOne(rs, User.class);
            req.setAttribute("user",user);
            req.getRequestDispatcher("/WEB-INF/user/update.jsp").forward(req,resp);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(rs);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
